import Cookies from 'js-cookie'

import { SESSION_KEY } from '@/utils/constants'

const TOKEN_KEY = 'newly_token'

export function getToken() {
  return Cookies.get(TOKEN_KEY)
}

export function setToken(token) {
  return Cookies.set(TOKEN_KEY, token)
}

export function removeToken() {
  return Cookies.remove(TOKEN_KEY)
}

// 存储租户至session中
export const setCacheTenant = (tenant) => {
  sessionStorage.setItem(SESSION_KEY.TENANT, JSON.stringify(tenant));
}

// 从session中获取租户
export const getCacheTenant = () => {
  const tenant = sessionStorage.getItem(SESSION_KEY.TENANT);
  if (tenant) {
    return JSON.parse(tenant);
  }
  return null;
}

// 从session中移除租户
export const clearCacheTenant = () => {
  sessionStorage.removeItem(SESSION_KEY.TENANT);
}

